package common

import "path"

const (
	linux_net_ns_base = "/run/netns/ns-mediumkube"
)

type Extension struct {
	Name  string   `yaml:"name"`
	Image string   `yaml:"image"`
	Port  string   `yaml:"port"`
	Args  []string `yaml:"args"`
}

type Overlay struct {
	Id               string  `yaml:"id"`
	Master           string  `yaml:"master"`
	EtcdPort         int     `yaml:"etcd-port"`
	GRPCPort         int     `yaml:"grpc-port"`
	Flannel          Flannel `yaml:"flannel"`
	Enabled          bool    `yaml:"enabled"`
	DNSEtcdPrefix    string  `yaml:"dns-etcd-prefix"`
	LeaseEtcdPrefix  string  `yaml:"lease-etcd-prefix"`
	DomainEtcdPrefix string  `yaml:"domain-etcd-prefix"`
	DLockEtcdPrefix  string  `yaml:"dlock-etcd-prefix"`
	Cidr             string  `yaml:"cidr"`
}

type Flannel struct {
	EtcdPrefix string `yaml:"etcd-prefix"`
	Iface      string `yaml:"iface"`
	Backend    string `yaml:"backend"`
	Network    string `yaml:"network"`
}

// NodeConfig Config for each noed. "node" field  in config yaml
type NodeConfig struct {
	Name string `yaml:"name"`
	CPU  string `yaml:"cpu"`
	MEM  string `yaml:"mem"`
	DISK string `yaml:"disk"`
	// CloudInit is full cloudinit string
	CloudInit string
	// Template is full libvirt template
	Template string
}

// Arg Argument
type Arg struct {
	Key   string `yaml:"key"`
	Value string `yaml:"value"`
}

// KubeInit Configuration for kubeadm init
type KubeInit struct {
	Args []Arg `yaml:"args"`
}

// Bridge that establish connections between vms
type Bridge struct {
	Name string `yaml:"name"`
	Inet string `yaml:"inet"`
}

type Flavor struct {
	Name     string `yaml:"name"`
	Template string `yaml:"template"`
}

// OverallConfig Over-all configs
/*
This config is in same file with Template config, but when parsed,
the template part will be ignored.
*/
type OverallConfig struct {
	HTTPSProxy      string      `yaml:"https-proxy,omitempty"`
	HTTPProxy       string      `yaml:"http-proxy,omitempty"`
	Backend         string      `yaml:"backend"`
	Bridge          Bridge      `yaml:"bridge"`
	Overlay         Overlay     `yaml:"overlay"`    // Overlay network configuration
	Extensions      []Extension `yaml:"extensions"` // Mediumkube extension
	Image           string      `yaml:"image"`
	CloudInit       string      `yaml:"cloud-init"`
	KubeInit        KubeInit    `yaml:"kube-init"`
	TmpDir          string      `yaml:"tmp_dir"`            // Directory to store temp files generated by medium kube
	VMKubeConfigDir string      `yaml:"vm_kube_config_dir"` // path of kubeconfig in virtual machine
	PubKeyDir       string      `yaml:"pub-key-dir"`
	PrivKeyDir      string      `yaml:"priv-key-dir"`
	HostPubKeyDir   string      `yaml:"host-pub-key-dir"`
	HostPrivKeyDir  string      `yaml:"host-priv-key-dir"` // Used for ssh execute
	Flavors         []Flavor    `yaml:"flavors"`
}

type KVMDomainConfig struct {
}

func (config OverallConfig) DomainNSFile() string {
	return path.Join(config.TmpDir, "dnsmasq.lease")
}

func (ext Extension) SnapshotID() string {
	return "snapshot-" + ext.Name
}

func (ext Extension) BridgeCNIID() string {
	return "cni-br-" + ext.Name
}

func (ext Extension) BridgeNetworkName() string {
	return "br-net-" + ext.Name
}

func (ext Extension) NetNS() string {
	return linux_net_ns_base + "-ext-" + ext.Name
}
